﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using CollegeShop.Api.DBUtility;
using System.Collections.Generic;//Please add references
namespace CollegeShop.Api.Infrastructure
{
	/// <summary>
	/// 数据访问类:t_goods
	/// </summary>
	public partial class T_GoodsDal
	{
		public T_GoodsDal()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId(int college)
		{
            return DbHelperMySQL.GetMaxID(college, "ID", "t_goods"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
        public bool Exists(int college, int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from t_goods");
			strSql.Append(" where ID=@ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ID", MySqlDbType.Int32)
			};
			parameters[0].Value = ID;

            return DbHelperMySQL.Exists(college, strSql.ToString(), parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
        public bool Add(int college,int client, CollegeShop.Api.Entity.T_Goods model)
        {
            List<CommandInfo> cmdList=new List<CommandInfo>();

            #region Resource SQL
            StringBuilder strSqlresource = new StringBuilder();
            strSqlresource.Append("insert into t_resource(");
            strSqlresource.Append("path,client)");
            strSqlresource.Append(" values (");
            strSqlresource.Append("@path,@client)");
            MySqlParameter[] parametersResource = {
					new MySqlParameter("@path", MySqlDbType.VarChar,512),
                    new MySqlParameter("@client", MySqlDbType.Int32),
                    new MySqlParameter("@ID", MySqlDbType.Int32)};
            parametersResource[0].Value = model.GoodsImage;
            parametersResource[1].Value = client;
            parametersResource[2].Direction = ParameterDirection.Output;
            #endregion

            cmdList.Add(new CommandInfo(strSqlresource.ToString(),parametersResource));
           
            #region goods SQL
            StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into t_goods(");
            strSql.Append("GoodsCode,GoodsName,GoodsNamePY,GoodsTitel,GoodsCategoryID,ResourceID,Standard,Unit,PackStandard,GoodsDescription,DefaultSalePrice,State)");
			strSql.Append(" values (");
            strSql.Append("@GoodsCode,@GoodsName,@GoodsNamePY,@GoodsTitel,@GoodsCategoryID,@ResourceID,@Standard,@Unit,@PackStandard,@GoodsDescription,@DefaultSalePrice,@State)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@GoodsCode", MySqlDbType.VarChar,50),
					new MySqlParameter("@GoodsName", MySqlDbType.VarChar,100),
					new MySqlParameter("@GoodsNamePY", MySqlDbType.VarChar,100),
					new MySqlParameter("@GoodsTitel", MySqlDbType.VarChar,1024),
					new MySqlParameter("@GoodsCategoryID", MySqlDbType.Int32,11),
					new MySqlParameter("@ResourceID", MySqlDbType.Int32,11),
					new MySqlParameter("@Standard", MySqlDbType.VarChar,20),
					new MySqlParameter("@Unit", MySqlDbType.VarChar,10),
					new MySqlParameter("@PackStandard", MySqlDbType.Int32,11),
					new MySqlParameter("@GoodsDescription", MySqlDbType.Text),
					new MySqlParameter("@DefaultSalePrice", MySqlDbType.Decimal,18),
					new MySqlParameter("@State", MySqlDbType.Int32,11)};
			parameters[0].Value = model.GoodsCode;
			parameters[1].Value = model.GoodsName;
			parameters[2].Value = model.GoodsNamePY;
			parameters[3].Value = model.GoodsTitel;
			parameters[4].Value = model.GoodsCategoryID;
            parameters[5].Direction = ParameterDirection.InputOutput;
			parameters[6].Value = model.Standard;
			parameters[7].Value = model.Unit;
			parameters[8].Value = model.PackStandard;
			parameters[9].Value = model.GoodsDescription;
			parameters[10].Value = model.DefaultSalePrice;
			parameters[11].Value = model.State;
            #endregion
            cmdList.Add(new CommandInfo(strSql.ToString(),parameters));

            try
            {
                DbHelperMySQL.ExecuteSqlTranWithIndentity(college, cmdList);
                return true;
            }
            catch (Exception ex) { 
            
            }
			
			return false;
			
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
        public bool Update(int college, int client, CollegeShop.Api.Entity.T_Goods model)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();

            #region Goods SQL
            StringBuilder strSql=new StringBuilder();
			strSql.Append("update t_goods set ");
			strSql.Append("GoodsCode=@GoodsCode,");
			strSql.Append("GoodsName=@GoodsName,");
			strSql.Append("GoodsNamePY=@GoodsNamePY,");
			strSql.Append("GoodsTitel=@GoodsTitel,");
			strSql.Append("GoodsCategoryID=@GoodsCategoryID,");
            strSql.Append("ResourceID=@ResourceID,");
			strSql.Append("Standard=@Standard,");
			strSql.Append("Unit=@Unit,");
			strSql.Append("PackStandard=@PackStandard,");
			strSql.Append("GoodsDescription=@GoodsDescription,");
			strSql.Append("DefaultSalePrice=@DefaultSalePrice,");
			strSql.Append("State=@State");
			strSql.Append(" where ID=@ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@GoodsCode", MySqlDbType.VarChar,50),
					new MySqlParameter("@GoodsName", MySqlDbType.VarChar,100),
					new MySqlParameter("@GoodsNamePY", MySqlDbType.VarChar,100),
					new MySqlParameter("@GoodsTitel", MySqlDbType.VarChar,1024),
					new MySqlParameter("@GoodsCategoryID", MySqlDbType.Int32,11),
					new MySqlParameter("@ResourceID", MySqlDbType.Int32,11),
					new MySqlParameter("@Standard", MySqlDbType.VarChar,20),
					new MySqlParameter("@Unit", MySqlDbType.VarChar,10),
					new MySqlParameter("@PackStandard", MySqlDbType.Int32,11),
					new MySqlParameter("@GoodsDescription", MySqlDbType.Text),
					new MySqlParameter("@DefaultSalePrice", MySqlDbType.Decimal,18),
					new MySqlParameter("@State", MySqlDbType.Int32,11),
					new MySqlParameter("@ID", MySqlDbType.Int32,11)};
			parameters[0].Value = model.GoodsCode;
			parameters[1].Value = model.GoodsName;
			parameters[2].Value = model.GoodsNamePY;
			parameters[3].Value = model.GoodsTitel;
			parameters[4].Value = model.GoodsCategoryID;
            parameters[5].Value = model.ResourceId;
			parameters[6].Value = model.Standard;
			parameters[7].Value = model.Unit;
			parameters[8].Value = model.PackStandard;
			parameters[9].Value = model.GoodsDescription;
			parameters[10].Value = model.DefaultSalePrice;
			parameters[11].Value = model.State;
			parameters[12].Value = model.ID;
            #endregion

            cmdList.Add(new CommandInfo(strSql.ToString(), parameters));

            #region Resource SQL
            StringBuilder strSqlResource = new StringBuilder();
            strSql.Append("update t_resource set ");
            strSql.Append("path=@path,client=@client");
            strSql.Append(" where ID=@ID");
            MySqlParameter[] parametersResource = {
					new MySqlParameter("@path", MySqlDbType.VarChar,512),
					new MySqlParameter("@client", MySqlDbType.Int32),
					new MySqlParameter("@ID", MySqlDbType.Int32,11)};
            parametersResource[0].Value = model.GoodsImage;
            parametersResource[1].Value = client;
            parametersResource[2].Value = model.ResourceId;
            cmdList.Add(new CommandInfo(strSqlResource.ToString(), parametersResource));
            #endregion

            int rows = DbHelperMySQL.ExecuteSqlTran(college, cmdList);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
        public bool Delete(int college, int ID,int resourceID)
		{
            List<CommandInfo> cmdList = new List<CommandInfo>();

			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_goods ");
			strSql.Append(" where ID=@ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ID", MySqlDbType.Int32)
			};
			parameters[0].Value = ID;
            cmdList.Add(new CommandInfo(strSql.ToString(), parameters));


            StringBuilder strSqlResource = new StringBuilder();
            strSqlResource.Append("delete from t_resource ");
            strSqlResource.Append(" where ID=@ID");
            MySqlParameter[] parametersResource = {
					new MySqlParameter("@ID", MySqlDbType.Int32)
			};
            parametersResource[0].Value = resourceID;
            cmdList.Add(new CommandInfo(strSqlResource.ToString(), parametersResource));

            int rows = DbHelperMySQL.ExecuteSqlTran(college,cmdList);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
        public bool DeleteList(int college, string IDs, string IDReources)
		{
			List<String> cmdList = new List<String>();

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_goods ");
            strSql.Append(" where ID in (" + IDs + ")  ");
            cmdList.Add(strSql.ToString());


            StringBuilder strSqlResource = new StringBuilder();
            strSqlResource.Append("delete from t_resource ");

            strSql.Append(" where ID in (" + IDReources + ")  ");
            cmdList.Add(strSqlResource.ToString());

            int rows = DbHelperMySQL.ExecuteSqlTran(college, cmdList);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public CollegeShop.Api.Entity.T_Goods GetModel(int college, int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select t_goods.ID,GoodsCode,GoodsName,GoodsNamePY,GoodsTitel,GoodsCategoryID,ResourceID,Path,Standard,Unit,PackStandard,GoodsDescription,DefaultSalePrice,State from t_goods ");
            strSql.Append(" left join t_resource on t_goods.ResourceID=t_resource.ID");
            strSql.Append(" where ID=@ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ID", MySqlDbType.Int32)
			};
			parameters[0].Value = ID;

			CollegeShop.Api.Entity.T_Goods model=new CollegeShop.Api.Entity.T_Goods();
            DataSet ds = DbHelperMySQL.Query(college, strSql.ToString(), parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public CollegeShop.Api.Entity.T_Goods DataRowToModel(DataRow row)
		{
			CollegeShop.Api.Entity.T_Goods model=new CollegeShop.Api.Entity.T_Goods();
			if (row != null)
			{
				if(row["ID"]!=null && row["ID"].ToString()!="")
				{
					model.ID=int.Parse(row["ID"].ToString());
				}
				if(row["GoodsCode"]!=null)
				{
					model.GoodsCode=row["GoodsCode"].ToString();
				}
				if(row["GoodsName"]!=null)
				{
					model.GoodsName=row["GoodsName"].ToString();
				}
				if(row["GoodsNamePY"]!=null)
				{
					model.GoodsNamePY=row["GoodsNamePY"].ToString();
				}
				if(row["GoodsTitel"]!=null)
				{
					model.GoodsTitel=row["GoodsTitel"].ToString();
				}
				if(row["GoodsCategoryID"]!=null && row["GoodsCategoryID"].ToString()!="")
				{
					model.GoodsCategoryID=int.Parse(row["GoodsCategoryID"].ToString());
				}
                if (row["ResourceId"] != null)
				{
                    model.ResourceId = Convert.ToInt32( row["ResourceId"]);
				}
                if (row["GoodsImage"] != null)
                {
                    model.GoodsImage = row["GoodsImage"].ToString();
                }
				if(row["Standard"]!=null)
				{
					model.Standard=row["Standard"].ToString();
				}
				if(row["Unit"]!=null)
				{
					model.Unit=row["Unit"].ToString();
				}
				if(row["PackStandard"]!=null && row["PackStandard"].ToString()!="")
				{
					model.PackStandard=int.Parse(row["PackStandard"].ToString());
				}
				if(row["GoodsDescription"]!=null)
				{
					model.GoodsDescription=row["GoodsDescription"].ToString();
				}
				if(row["DefaultSalePrice"]!=null && row["DefaultSalePrice"].ToString()!="")
				{
					model.DefaultSalePrice=decimal.Parse(row["DefaultSalePrice"].ToString());
				}
				if(row["State"]!=null && row["State"].ToString()!="")
				{
					model.State=int.Parse(row["State"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
        public DataSet GetList(int college, string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select t_goods.ID,GoodsCode,GoodsName,GoodsNamePY,GoodsTitel,GoodsCategoryID,ResourceID,Path,Standard,Unit,PackStandard,GoodsDescription,DefaultSalePrice,State from t_goods ");
            strSql.Append(" left join t_resource on t_goods.ResourceID=t_resource.ID ");
            
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            return DbHelperMySQL.Query(college, strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
        public int GetRecordCount(int college, string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select count(1) FROM t_goods "); 
            strSql.Append(" left join t_resource on t_goods.ResourceID=t_resource.ID ");
			if(strWhere.Trim().Length>0)
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperMySQL.GetSingle(college, strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
        public DataSet GetListByPage(int college, string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ID desc");
			}
            strSql.Append(")AS Row, T.*  from t_goods T ");
            strSql.Append(" left join t_resource on t_goods.ResourceID=t_resource.ID ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(college, strSql.ToString());
		}

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

